import {Component, EventEmitter, forwardRef, Input, Output} from '@angular/core';
import {NG_VALUE_ACCESSOR} from "@angular/forms";

@Component({
    selector: 'son',
    templateUrl: 'son.html',
    providers: [{
        provide: NG_VALUE_ACCESSOR,
        useExisting: forwardRef(() => SonComponent),
        multi: true
    }]
})
export class SonComponent {

    @Input('fatherInput')fatherInput:string;

    @Output('changeFather')changeFather=new EventEmitter<any>();

    constructor() {
    }

    changeInput(){
        this.fatherInput='changed fatherInput'
    }

    emitOutput(){
        this.changeFather.emit(this.fatherInput)
    }

    private modelVal: any;

    public onModelChange: Function = () => {};
    public onModelTouched: Function = () => {};
    writeValue(value: any) {
        this.modelVal = value;
    }
    registerOnChange(fn: Function): void {
        this.onModelChange = fn;
    }
    registerOnTouched(fn: Function): void {
        this.onModelTouched = fn;
    }

    changeModel() {
        this.modelVal = 'changed modelVal';
        this.onModelChange(this.modelVal);
    }

}
